package studentsys;

public class StudentService {


    private Student[] students; // 数组是Java中最基本的数据容器
    private int StudentNum = 1;
    private int idCount = 1;

    public StudentService(int size) {
        students = new Student[size];
        students[0] = new Student(1, "小明", 16);
    }

    //findById方法 返回House对象或null
    public Student findById(int findId) {

        for (int i = 0; i < StudentNum; i++) {
            if (findId == students[i].getId()) {
                return students[i];
            }
        }
        return null;
    }


    //add方法，添加对象，返回boole
    public boolean add(Student newStudent) {
        if (StudentNum == students.length) {
            System.out.println("数组已满，不能再添加");
            return false;
        }
        //添加到数组最后
        students[StudentNum++] = newStudent;
        idCount++;
        newStudent.setId(idCount);
        return true;
    }

    //del方法 删除一个学生信息
    public boolean del(int delId){
        int index = -1;
        for (int i = 0; i < StudentNum; i++) {
            if (delId == students[i].getId()){
                index = i;
            }
        }
        if (index == -1){
            return false;
        }

        for (int i = index; i < StudentNum - 1; i++) {
            students[i] = students[i + 1];
        }
        students[--StudentNum] = null;
        return true;
    }


    public Student[] list() {
        return students;
    }

}
